package com.cuz.series.linklist;

/**
 * https://leetcode.cn/problems/reverse-linked-list/
 */
public class 链表1反转单链表 {


    public static ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        //哑节点，
        ListNode preHead = new ListNode();
        preHead.next = head;
        //当前遍历到的节点
        ListNode cur = head.next;
        //当前遍历到节点的前置节点
        ListNode curPre = head;

        while (cur != null) {

            //断开cur的指针
            curPre.next = cur.next;

            //cur 插入到preHead中间。
            cur.next = preHead.next;
            preHead.next = cur;

            //继续
            cur = curPre.next;
        }

        return preHead.next;
    }

    public static void main(String[] args) {
        ListNode of = ListNode.of1(1, 2, 3, 4, 5, 6);
        of.print();
        reverseList(of).print();
    }
}
